package com.ruoyi.wzgl.cgjh.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EasyExcelUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.wzgl.cgjh.domain.Cgjh;
import com.ruoyi.wzgl.cgjh.service.ICgjhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购计划Controller
 *
 * @author huacai
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/wzgl/cgjh")
public class CgjhController extends BaseController {
    @Autowired
    private ICgjhService cgjhService;

    @Autowired
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cgjh cgjh) {
        startPage();
        List<Cgjh> list = cgjhService.selectCgjhList(cgjh);
        return getDataTable(list);
    }

    /**
     * 导出采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:export')")
    @Log(title = "采购计划" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cgjh cgjh) {
        List<Cgjh> list = cgjhService.selectCgjhList(cgjh);
        ExcelUtil<Cgjh> util = new ExcelUtil<Cgjh>(Cgjh.class);
        util.exportExcel(response, list, "采购计划数据");
    }

    /**
     * 根据模板导出excel
     */
    @PostMapping("/exportWithTemplates")
    public void exportWithTemplate(HttpServletResponse response, Cgjh cgjh) {
        System.out.println(cgjh);
        String cgjhId = cgjh.getCgjhId();
        // 准备导出数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("cgjhId", cgjhId);

        // 调用导出工具
        EasyExcelUtil.exportWithTemplate(response, dataMap, "test.xlsx", "用户数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Cgjh> util = new ExcelUtil<Cgjh>(Cgjh.class);
        util.importTemplateExcel(response, "采购计划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "采购计划" , businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Cgjh> util = new ExcelUtil<Cgjh>(Cgjh.class);
        InputStream inputStream = file.getInputStream();
        List<Cgjh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = cgjhService.batchInsertCgjh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取采购计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:query')")
    @GetMapping(value = "/{cgjhId}")
    public AjaxResult getInfo(@PathVariable("cgjhId") String cgjhId) {
        return success(cgjhService.selectCgjhByCgjhId(cgjhId));
    }

    /**
     * 新增采购计划
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:add')")
    @Log(title = "采购计划" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cgjh cgjh) {
        int insertCgjh = cgjhService.insertCgjh(cgjh);

        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(cgjh.getCgjhId());
        infoRunTaskData.setBusinessType(14); //业务模块: 采购计划单申请
        infoRunTaskData.setBusinessTitle("采购计划单"); //任务名称: 采购计划单
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人
        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(insertCgjh);
    }

    /**
     * 修改采购计划
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:edit')")
    @Log(title = "采购计划" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cgjh cgjh) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(cgjh.getCgjhId());
        infoRunTaskData.setBusinessType(14); //业务模块: 采购计划单申请
        infoRunTaskData.setBusinessTitle("采购计划单"); //任务名称: 采购计划单
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData);

        return toAjax(cgjhService.updateCgjh(cgjh));
    }

    /**
     * 删除采购计划
     */
    @PreAuthorize("@ss.hasPermi('wzgl:cgjh:remove')")
    @Log(title = "采购计划" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{cgjhIds}")
    public AjaxResult remove(@PathVariable String[] cgjhIds) {
        //同时删除业务关系数据
        for (String cgjhId : cgjhIds) {
            Cgjh cgjh = cgjhService.selectCgjhByCgjhId(cgjhId);
            iInfoRunTaskDataService.deleteData(cgjh.getCgjhId(), 14);//业务模块: 采购计划单申请
        }
        return toAjax(cgjhService.deleteCgjhByCgjhIds(cgjhIds));
    }
}
